package com.smart.home.controller;

import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.smart.home.model.PicModel;
import com.smart.home.service.UpPicService;
import com.smart.home.system.DataGrid;
import com.smart.home.system.Result;
import com.smart.home.system.SystemConfig;

@Controller
@RequestMapping("up")
public class UpPicController extends BaseController{
	private static final Logger logger = Logger.getLogger(UpPicController.class);
	
	@Autowired
	private UpPicService picService;
	
	/**
	 * 查询图片列表
	 * @param type
	 * @param isAble
	 * @return
	 */
	@RequestMapping("/getPicList")
	@ResponseBody
	public DataGrid getPicList(Integer type,Integer isAble,Integer page,Integer rows){
		DataGrid grid = new DataGrid();
		Map<String, Object> params = new HashMap<String, Object>();
		if(page ==null || rows == null || page < 1 || rows < 1){
			return grid;
		}
		params.put("page", (page-1)*rows);
		params.put("rows", rows);
		if(type != null){
			params.put("type", type);
		}
		if(isAble != null){
			params.put("isAble", isAble);
		}
		try {
			params.put("path", SystemConfig.getResource("picPath")+"/");
			grid = picService.getPicList(params);
		} catch (Exception e) {
			logger.error("获取图片列表失败："+e.getMessage());
		}
		return grid;
	}
	
	
	/**
	 * 上传图片
	 * @param picModel
	 * @return
	 */
	@RequestMapping("/uploadPic")
	@ResponseBody
	public Result uploadPic(PicModel picModel){
		Result result = new Result();
		File real = new File(Thread.currentThread().getContextClassLoader().getResource("").getPath().toString());
		String realPath = real.getParentFile().getParentFile().getParent();
		MultipartFile file = picModel.getSourceFile();
		String picName = "";
		picName = picModel.getType() + RandomStringUtils.randomAlphanumeric(10) + picModel.getCover().substring(picModel.getCover().lastIndexOf("."));
		try {
			String filePath = SystemConfig.getResource("picPath").toString().trim();
			File file2 = new File(realPath+"/"+filePath,picName);
			if(!file2.exists()){
				if(!file2.getParentFile().exists()){
					file2.mkdir();
				}
				file2.createNewFile();
			}else{
				picName = picModel.getType() + RandomStringUtils.randomAlphanumeric(10) + picModel.getCover().substring(picModel.getCover().lastIndexOf("."));
				file2 = new File(realPath+"/"+filePath,picName);
				file2.createNewFile();
			}
			FileUtils.copyInputStreamToFile(file.getInputStream(), file2);
			picModel.setPath(picName);
			picModel.setCreateTime(new Date());
			picService.addPic(picModel);
			result.setMsg("上传成功！");
			result.setResultCode(Result.SUCC);
		} catch (Exception e) {
			logger.error("更改图片状态："+e.getMessage());
			result.setMsg("系统错误！");
		}
		return result;
	}
	
	/**
	 * 更新图片状态
	 * @param picId
	 * @param isAble
	 * @return
	 */
	@RequestMapping("/updatePicStatus")
	@ResponseBody
	public Result updatePicStatus(Integer picId,Integer isAble){
		Result result = new Result();
		if(isAble == null || picId == null){
			result.setMsg("缺少参数！");
			return result;
		}
		try {
			picService.updatePicStatus(picId,isAble);
			result.setMsg("更改成功！");
			result.setResultCode(Result.SUCC);
		} catch (Exception e) {
			logger.error("更改图片状态："+e.getMessage());
			result.setMsg("系统错误！");
		}
		return result;
	}
	
	/**
	 * 删除图片
	 * @param picId
	 * @param picPath
	 * @return
	 */
	@RequestMapping("/deletePic")
	@ResponseBody
	public Result deletePic(Integer picId,String picPath){
		Result result = new Result();
		if(picId == null || StringUtils.isBlank(picPath)){
			result.setMsg("缺少参数");
			return result;
		}
		File file = new File(picPath);
		file.delete();
		try {
			picService.delPic(picId);
			result.setMsg("删除成功！");
			result.setResultCode(Result.SUCC);
		} catch (Exception e) {
			logger.error("删除图片："+e.getMessage());
			result.setMsg("系统错误！");
		}
		return result;
	}

}
